package rexsee.sensor;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class RexseeSensorTemperature implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Temperature";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeSensorTemperature(childBrowser);
	}

	public static final String EVENT_ONTEMPERATURECHANGED = "onTemperatureChanged";

	private final Context mContext;
	private final Browser mBrowser;
	private final SensorManager mSensorManager;
	private final SensorEventListener mSensorListener;
	private final Sensor mSensor;

	private int mRate = SensorManager.SENSOR_DELAY_NORMAL;
	private int mCycle = 100; //milliseconds
	private int mEventCycle = 100; //milliseconds
	private float mAccuracy = 0;

	private long lastUpdate = -1;
	private long lastEvent = -1;

	private float value = -999f;

	public RexseeSensorTemperature(Browser browser) {
		mContext = browser.getContext();
		mBrowser = browser;
		browser.eventList.add(EVENT_ONTEMPERATURECHANGED);

		mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);

		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);

		mSensorListener = new SensorEventListener() {
			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
			}
			@Override
			public void onSensorChanged(SensorEvent event) {
				if (event.sensor.getType() != Sensor.TYPE_TEMPERATURE) return;
				long curTime = System.currentTimeMillis();
				if (lastUpdate == -1 || (curTime - lastUpdate) > mCycle) {
					lastUpdate = curTime;
					float lastValue = value;
					value = event.values[SensorManager.DATA_X];
					if (lastEvent == -1 || (curTime - lastEvent) > mEventCycle) {
						if (Math.abs(value - lastValue) > mAccuracy) {
							lastEvent = curTime;
							mBrowser.eventList.run(EVENT_ONTEMPERATURECHANGED);
						}
					}
				}
			}
		};

	}

	public String getLastKnownValue() {
		return (value == -999) ? "null" : String.valueOf(value);
	}

	public void setRate(String rate) {
		mRate = SensorRate.getInt(rate);
	}
	public String getRate() {
		return SensorRate.getString(mRate);
	}
	public void setCycle(int milliseconds) {
		mCycle = milliseconds;
	}
	public int getCycle() {
		return mCycle;
	}
	public void setEventCycle(int milliseconds) {
		mEventCycle = milliseconds;
	}
	public int getEventCycle() {
		return mEventCycle;
	}
	public void setAccuracy(float value) {
		mAccuracy = Math.abs(value);
	}
	public float getAccuracy() {
		return mAccuracy;
	}

	public boolean isReady() {
		return (mSensor == null) ? false : true;
	}
	public void start() {
		if (isReady()) {
			mSensorManager.registerListener(mSensorListener, mSensor, mRate);
		} else {
			mBrowser.exception(getInterfaceName(), "Temperature sensor is not found.");
		}
	}
	public void stop() {
		if (isReady()) {
			mSensorManager.unregisterListener(mSensorListener);
		}
	}

}
